package com.android.tetris;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.android.tetris.utils.Values;

/**
 * Game board: vung nguoi choi
 * 
 * @author Administrator
 * 
 */
public class GameView extends View {

	// Mang chua cac khoi block de cau thanh cac vien gach
	private Drawable[][] block = new Drawable[Values.NUM_ROW][Values.NUM_COL];

	// Mang chua cac block de tao cac duong bien (left, right, floor)
	public Drawable[] wall = new Drawable[102];
	
	// Context va canvas de tao game board
	private Context context;
	private Canvas c;

	// Toa do va kich thuoc ban dau cua Wall
	private int leftWall;
	private int topWall;
	private int sizeWall;

	/**
	 * Dinh nghia content va canvas
	 * 
	 * @param cont
	 * @param attrs
	 */
	public GameView(Context cont, AttributeSet attrs) {
		super(cont, attrs);
		context = cont;
	}

	public void initialize(int i, int j, int left, int top, int size) {
		block[i][j] = context.getResources().getDrawable(R.drawable.alpha);
		block[i][j].setBounds(left, top, left + size, top + size);
	}

	/**
	 * Khoi tao 2 duong bien
	 * 
	 * @param left
	 * @param top
	 * @param size
	 */
	public void createWall(int left, int top, int size){
		int i = 0, x, y;
		x = left - size / 2;
		y = top;
		//The left wall
		while (i < 40){
			wall[i] = context.getResources().getDrawable(R.drawable.brick);
			wall[i].setBounds(x, y, x + size / 2, y + size / 2);
			y = y + size / 2;
			i = i + 1;
		}
		x = left + size * 10;
		y = top;
		//The right wall
		while (i < 80){
			wall[i] = context.getResources().getDrawable(R.drawable.brick);
			wall[i].setBounds(x, y, x + size / 2, y + size / 2);
			y = y + size / 2;
			i = i + 1;
		}		
		x = left - size / 2;
		//The floor
		while (i < 102){
			wall[i] = context.getResources().getDrawable(R.drawable.brick);
			wall[i].setBounds(x, y, x + size / 2, y + size / 2);
			x = x + size / 2;
			i = i + 1;
		}		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		c = canvas;
		super.onDraw(canvas);

		// Ve Wall
		for (int i = 0; i < 102; i++){
        	wall[i].draw(c);
		}

		for (int i = 0; i < Values.NUM_ROW; i++) {
			for (int j = 0; j < Values.NUM_COL; j++) {
				block[i][j].draw(canvas);
			}
		}
		// Actually draw
		invalidate();
	}

	/**
	 * Property de lay canvas
	 * 
	 * @return
	 */
	public Canvas getCanvas() {
		return c;
	}

	/**
	 * Xac dinh mau cua 1 vien gach
	 * 
	 * @param i
	 * @param j
	 * @param c
	 */
	public void setColor(int i, int j, byte c) {
		Rect rect;
		rect = block[i][j].getBounds();
		switch (c) {
		case Values.COLOR_NONE:
			block[i][j] = context.getResources().getDrawable(R.drawable.alpha);
			break;
		case Values.COLOR_RED:
			block[i][j] = context.getResources().getDrawable(
					R.drawable.block_red);
			break;
		case Values.COLOR_GREEN:
			block[i][j] = context.getResources().getDrawable(
					R.drawable.block_green);
			break;
		case Values.COLOR_BLUE:
			block[i][j] = context.getResources().getDrawable(
					R.drawable.block_blue);
			break;
		case Values.COLOR_YELLOW:
			block[i][j] = context.getResources().getDrawable(
					R.drawable.block_yellow);
			break;
		case Values.COLOR_PINK:
			block[i][j] = context.getResources().getDrawable(
					R.drawable.block_pink);
			break;
		case Values.COLOR_PURPLE:
			block[i][j] = context.getResources().getDrawable(
					R.drawable.block_purple);
			break;
		case Values.COLOR_WHITE:
			block[i][j] = context.getResources().getDrawable(
					R.drawable.block_white);
			break;
		}
		block[i][j].setBounds(rect);
	}

	public int getLeftWall() {
		return leftWall;
	}

	public void setLeftWall(int leftWall) {
		this.leftWall = leftWall;
	}

	public int getTopWall() {
		return topWall;
	}

	public void setTopWall(int topWall) {
		this.topWall = topWall;
	}

	public int getSizeWall() {
		return sizeWall;
	}

	public void setSizeWall(int sizeWall) {
		this.sizeWall = sizeWall;
	}
}
